package principale.persistenza.database;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;

import principale.domain.InterfacciaDomain;
import principale.persistenza.dao.PersonaDao;

public class PersonaDaoDatabase implements PersonaDao {

	private Connection connect;
	private Statement statement;
	private ResultSet resultSet;

	public PersonaDaoDatabase() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connect = (Connection) DriverManager.getConnection(""
					+ "jdbc:mysql://localhost/rubrica?user=admin&password=admin");
			statement = (Statement) connect.createStatement();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void aggiungi(Vector elemento, String tipo) {
		int row=elemento.size()-1;
		String[] dati=((InterfacciaDomain)elemento.get(row)).getDati();
		String sqlStatement="insert into rubrica." + tipo + " values (";
		for(String data: dati) {
			sqlStatement+="'"+data+"',";
		}
		sqlStatement+=row+");";
		try {
			statement.execute(sqlStatement);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void modifica(Vector elemento, int row, String tipo) {
		// TODO Auto-generated method stub
		String[] dati=((InterfacciaDomain)elemento.get(row)).getDati();
		String sqlStatement="replace into rubrica." + tipo + " values (";
		for(String data: dati) {
			sqlStatement+="'"+data+"',";
		}
		sqlStatement+=row+");";
		try {
			statement.execute(sqlStatement);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void elimina(Vector elemento, int row, String tipo) {
		String sqlStatement1="delete from rubrica."+tipo+" where row="+row+";";
		String sqlStatement2="update rubrica."+tipo+" set row=row-1 where row>"+row+";";
		try {
			statement.execute(sqlStatement1);
			statement.execute(sqlStatement2);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Vector<String[]> carica(String tipo) {
		// TODO Auto-generated method stub
		Vector<String[]> elemento=new Vector<String[]>();
		try {
			resultSet=statement.executeQuery("select * from rubrica."+tipo+" order by row");

			ResultSetMetaData rsmd = (ResultSetMetaData) resultSet.getMetaData();
			int numeroColonne;
			numeroColonne = rsmd.getColumnCount();
			while(resultSet.next()) {
				String[] dati=new String[numeroColonne-1];
				for (int i=0;i<dati.length;i++) {
					dati[i]=resultSet.getString(i+1);
				}
				elemento.add(dati);
			}
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return elemento;
	}
}
